﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIObjects.TreeViewInterfaces;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using Entities = AbstraX.BindingsTreeEntities;
using System.Collections.ObjectModel;
using AbstraX.Contracts;
using MvvmTreeView;
using System.ServiceModel.DomainServices.Client;

namespace AbstraX.ViewModels.BindingsTree
{
    public class BindingsTreeNodeDataContext : BindingsTreeNodeBase
    {
        private Entities.DataContextObject dataContext;

        public BindingsTreeNodeDataContext(IBindingsTreeService bindingsTreeService, ITreeView treeView, Entities.DataContextObject dataContext) : base(bindingsTreeService, treeView)
        {
            this.dataContext = dataContext;
            this.Tag = dataContext;
        }

        public override void LoadChildren()
        {
            var dataContext = (Entities.DataContextObject)this.Tag;

#if SILVERLIGHT
            var children = bindingsTreeService.LoadChildren<Entities.DataContextObject>(dataContext);

            children.CollectionChanged += (s, e) =>
            {
                ChildrenLoaded(children);
            };

#endif
            treeView.SetBusy(this, true);
        }

        public override void OnSelect()
        {
        }

        public override Entity Entity
        {
            get
            {
#if SILVERLIGHT
                return dataContext;
#else
                return null;
#endif
            }
        }

        protected override void ChildrenLoaded(IBindingsTreeCollection children)
        {
            isChildrenLoaded = true;

            this.RemoveSpinster();

            base.AddChildren(children);

            treeView.SetBusy(this, false);
        }

        public override string Text
        {
            get
            {
                return dataContext.GetName();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override List<string> AddNewShortCuts
        {
            get { throw new NotImplementedException(); }
        }

        public override void AddNew(string shortCut)
        {
            throw new NotImplementedException();
        }

        public override void DragStarting(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void DragCompleted(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void ItemDropped(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e, int insertionIndex)
        {
        }

        public override void GiveFeedback(object sender, UIObjects.DragDropInterfaces.GiveFeedbackEventArgs e)
        {
        }

        public override void DragEnter(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragLeave(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragOver(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override float ChildOrdinal
        {
            get
            {
                return dataContext.GetChildOrdinal();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool? HasChildren
        {
            get
            {
                return dataContext.GetHasChildren();
            }
        }
    }
}
